import { HikvisionBaseService } from "../base";
import { hikvision } from "../type";

/**
 * 资源 - 人员信息管理 API
 */
export class ResourcePersonService extends HikvisionBaseService {
	/**
	 * 获取资源属性，这是isc平台的自定义的属性
	 * - 如人员信息哪些字段必填，需要先从此查询出来，然后才添加人员
	 */
	async getResourceProperty(resourceType: "person" | "region" | "vehicle" | "organization") {
		const url = `${this.config.baseUrl}/api/resource/v1/resource/properties`;
		const res = await this.curl<hikvision.isc.IResponse<hikvision.isc.IResourcePropertyObject[]>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				resourceType,
			},
			headers: {
				access_token: this.accessToken,
			},
		});

		this.handleResponseException(res, `获取资源属性出错`);
		return res.data.data;
	}

	/**
	 * 单个新增人员信息
	 */
	async add(params: hikvision.isc.IAddPersonParams) {
		const url = `${this.config.baseUrl}/api/resource/v2/person/single/add`;
		const res = await this.curl<hikvision.isc.IResponse<{ personId: string; faceId: string }>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				...params,
			},
			headers: {
				access_token: this.accessToken,
			},
		});

		this.handleResponseException(res, `单个新增人员出错`);
		return res.data.data;
	}

	/** 修改人员 */
	async update(params: hikvision.isc.IUpdatePersonParams) {
		const url = `${this.config.baseUrl}/api/resource/v1/person/single/update`;
		const res = await this.curl<hikvision.isc.IResponse<null>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				...params,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `修改人员出错`);

		return true;
	}

	/** 批量删除人员 */
	async batchDelete(personIds: string[]) {
		const url = `${this.config.baseUrl}/api/resource/v1/person/batch/delete`;
		type ResObj = { personId: string; code: string; msg: string };
		const res = await this.curl<hikvision.isc.IResponse<ResObj[]>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				personIds,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `批量删除人员出错`);
		return res.data.data;
	}

	/**
	 * 单个添加人脸照片
	 *
	 * @param {string} personId 人员ID
	 * @param {string} faceData 人脸照片base64编码
	 * @return {*}
	 * @memberof ResourcePersonService
	 */
	async addFace(personId: string, faceData: string) {
		const url = `${this.config.baseUrl}/api/resource/v1/face/single/add`;
		type ResObj = { faceId: string; faceUrl: string; personId: string };
		const res = await this.curl<hikvision.isc.IResponse<ResObj>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				personId,
				faceData,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `单个添加人脸照片出错`);
		return res.data.data;
	}

	/**
	 * 单个修改人脸照片
	 *
	 * @param {string} personId 人员ID
	 * @param {string} faceData 人脸照片base64编码
	 * @return {*}
	 * @memberof ResourcePersonService
	 */
	async updateFace(personId: string, faceData: string) {
		const url = `${this.config.baseUrl}/api/resource/v1/face/single/update`;
		type ResObj = { faceId: string; faceUrl: string; personId: string };
		const res = await this.curl<hikvision.isc.IResponse<ResObj>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				personId,
				faceData,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `单个修改人脸照片出错`);
		return res.data.data;
	}

	/** 删除人脸照片 */
	async deleteFace(faceId: string) {
		const url = `${this.config.baseUrl}/api/resource/v1/face/single/delete`;
		const res = await this.curl<hikvision.isc.IResponse<null>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				faceId,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `单个修改人脸照片出错`);
		return true;
	}

	/** 获取人员列表（v2） */
	async getPersonList(pageNo: number, pageSize = 100) {
		const url = `${this.config.baseUrl}/api/resource/v2/person/personList`;
		const res = await this.curl<hikvision.isc.IResponse<hikvision.isc.IPersonObject>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				pageNo,
				pageSize,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `获取人员列表出错`);
		return res.data.data;
	}

	/** 查询人员列表（v2）支持按条件搜索 */
	async getPersonListByAdvance(params: hikvision.isc.IPersonSearchRequest) {
		const url = `${this.config.baseUrl}/api/resource/v2/person/advance/personList`;
		const res = await this.curl<hikvision.isc.IResponse<hikvision.isc.IPersonObject>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				...params,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `查询人员列表出错`);
		return res.data.data;
	}

	/** 获取人员详情
	 */
	async getPersonInfoDetail(params: { paramName: "certificateNo" | "personId" | "phoneNo" | "jobNo"; paramValue: string[] }) {
		/*  参数名 certificateNo：证件号码，从[获取人员列表v2]@[软件产品-食堂消费管理平台-API列表-资源目录-人员信息接口-人员及照片接口#获取人 员列表v2]接口返回报文中的certificateNo字段；
		 *  personId：人员Id，从[获取人员列表v2]@[软件产品-食堂消费管理平台-API列表-资源目录-人员信息接口-人员及照片接口#获取人 员列表v2]接口返回报文中的personId字段；
		 *  phoneNo：手机号码，从[获取人员列表v2]@[软件产品-食堂消费管理平台-API列表-资源目录-人员信息接口-人员及照片接口#获取人 员列表v2]接口返回报文中的phoneNo字段；
		 *  jobNo：工号，从[获取人员列表v2]@[软件产品-食堂消费管理平台-API列表-资源目录-人员信息接口-人员及照片接口#获取人 员列表v2]接口返回报文中的jobNo字段。
		 */
		const url = `${this.config.baseUrl}/api/resource/v1/person/condition/personInfo`;
		const res = await this.curl<hikvision.isc.IResponse<{ total: number; list: hikvision.isc.IPersonInfoDetail[] }>>(url, {
			method: "POST",
			dataType: "json",
			contentType: "json",
			data: {
				...params,
			},
			headers: {
				access_token: this.accessToken,
			},
		});
		this.handleResponseException(res, `获取人员详情出错`);
		return res.data.data;
	}
}
